home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / General / Appsprites / USER CODE / Simple.c < prev    next >
Text File  |  1993-07-11  |  6KB  |  268 lines

  1. ///--------------------------------------------------------------------------------------
  2. // Simple.c
  3. //
  4. // By: Tony Myles
  5. //
  6. // Copyright © 1993 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef __DESK__
  11. #include <Desk.h>
  12. #endif
  13.  
  14. #ifndef __EVENTS__
  15. #include <Events.h>
  16. #endif
  17.  
  18. #ifndef __OSEVENTS__
  19. #include <OSEvents.h>
  20. #endif
  21.  
  22. #ifndef __TOOLUTILS__
  23. #include <ToolUtils.h>
  24. #endif
  25.  
  26. #ifndef __SEGLOAD__
  27. #include <SegLoad.h>
  28. #endif
  29.  
  30. #ifndef __WINDOWS__
  31. #include <Windows.h>
  32. #endif
  33.  
  34. #ifndef __SPRITEWORLD__
  35. #include <SpriteWorld.h>
  36. #endif
  37.  
  38. #ifndef __SPRITELAYER__
  39. #include <SpriteLayer.h>
  40. #endif
  41.  
  42. #ifndef __SPRITE__
  43. #include <Sprite.h>
  44. #endif
  45.  
  46. #ifndef __FRAME__
  47. #include <Frame.h>
  48. #endif
  49.  
  50. #ifndef __SPRITEWORLDUTILS__
  51. #include <SpriteWorldUtils.h>
  52. #endif
  53.  
  54. #ifndef __APPLICATION__
  55. #include "Application.h"
  56. #endif
  57.  
  58. #ifndef __SIMPLE__
  59. #include "Simple.h"
  60. #endif
  61.  
  62. #include "Globals.h"
  63.  
  64. ///--------------------------------------------------------------------------------------
  65. // PrepareSimpleAnimation
  66. ///--------------------------------------------------------------------------------------
  67.  
  68. void PrepareSimpleAnimation (void)
  69. {
  70.     OSErr err;
  71.     GrafPtr savePort;
  72.  
  73.     GetPort(&savePort);
  74.     SetPort((GrafPtr)curWindow);
  75.     SetCursor(*GetCursor(watchCursor));
  76.  
  77.  
  78.     //
  79.     // STEP #1: initialize the sprite world package
  80.     //
  81.  
  82.     err = SWEnterSpriteWorld();
  83.     FatalError(err);
  84.  
  85.  
  86.     //
  87.     // STEP #2: create the various pieces that we need
  88.     //
  89.  
  90.         // create the sprite world
  91.     err = SWCreateSpriteWorldFromWindow(&(cur->spriteWorldP), (CWindowPtr)curWindow, NULL);
  92.     FatalError(err);
  93.  
  94.         // create the sprite layer
  95.     err = SWCreateSpriteLayer(&(cur->spriteLayerP));
  96.     FatalError(err);
  97.  
  98.         // create the first sprite
  99.     err = SWCreateSpriteFromPictResource(&(cur->simpleSpriteP), NULL, 1000,1000,
  100.             6, kRegionMask);
  101.     FatalError(err);
  102.     cur->simpleSpriteArray[0] = cur->simpleSpriteP;
  103.  
  104.         // clone the rest of the sprites off the first one
  105.       for (cur->spriteNum = 1; cur->spriteNum < kNumberOfSprites; cur->spriteNum++)
  106.     {
  107.         err = SWCloneSprite(cur->simpleSpriteP, cur->simpleSpriteArray + cur->spriteNum, NULL);
  108.         FatalError(err);
  109.     }
  110.  
  111.  
  112.     //
  113.     // STEP #3: put the pieces together (must be done BEFORE the sprite world is locked!)
  114.     //
  115.  
  116.     for (cur->spriteNum = 0; cur->spriteNum < kNumberOfSprites; cur->spriteNum++)
  117.     {
  118.             // add the sprite to the layer
  119.         SWAddSprite(cur->spriteLayerP, cur->simpleSpriteArray[cur->spriteNum]);
  120.     }
  121.  
  122.         // add the layer to the world
  123.     SWAddSpriteLayer(cur->spriteWorldP, cur->spriteLayerP);
  124.  
  125.  
  126.     //
  127.     // STEP #4: set things up for the animation
  128.     //
  129.  
  130.         // calculate the movement boundary rectangle
  131.     cur->moveBoundsRect = cur->spriteWorldP->windowFrameP->frameRect;
  132.  
  133.     for (cur->spriteNum = 0; cur->spriteNum < kNumberOfSprites; cur->spriteNum++)
  134.     {
  135.         cur->simpleSpriteP = cur->simpleSpriteArray[cur->spriteNum];
  136.         cur->moveSpeed = cur->moveSpeedArray[cur->spriteNum];
  137.  
  138.             // set up the sprite
  139.         SWSetSpriteMoveBounds(cur->simpleSpriteP, &(cur->moveBoundsRect));
  140.         SWSetSpriteMoveTime(cur->simpleSpriteP, (long)cur->moveSpeed);
  141.         SWSetSpriteMoveProc(cur->simpleSpriteP, SWBounceSpriteMoveProc);
  142.         SWSetSpriteMoveDelta(cur->simpleSpriteP, kHorizMoveDelta, kVertMoveDelta);
  143.         
  144.         SWSetSpriteFrameAdvance(cur->simpleSpriteP, 1);
  145.         SWSetSpriteFrameRange(cur->simpleSpriteP, 1,5);
  146.         SWSetCurrentFrameIndex(cur->simpleSpriteP, 0);
  147.         SWSetSpriteFrameTime(cur->simpleSpriteP, (long)cur->moveSpeed);
  148.  
  149.             // set the sprite’s initial location
  150.         SWSetSpriteLocation(cur->simpleSpriteP, cur->spriteNum * 60, cur->spriteNum * 60);
  151.     }
  152.  
  153.  
  154.  
  155.  
  156.     //
  157.     // STEP #5: lock the sprite world        !!! VERY IMPORTANT !!!
  158.     //
  159.  
  160.     SWLockSpriteWorld(cur->spriteWorldP);
  161.  
  162.  
  163.  
  164.     SetPort(savePort);
  165.     ShowCursor();
  166.     SetCursor(&qd.arrow);
  167.  
  168. }
  169.  
  170.  
  171.  
  172.  
  173. ///--------------------------------------------------------------------------------------
  174. // PerformSimpleAnimation
  175. ///--------------------------------------------------------------------------------------
  176.  
  177. void PerformSimpleAnimation (void)
  178. {
  179.     GrafPtr savePort;
  180.  
  181.     GetPort(&savePort);
  182.     SetPort((GrafPtr)curWindow);
  183.  
  184.     if (curWindow != 0){
  185.  
  186.     //
  187.     // STEP #6: run the animation
  188.     //
  189.  
  190.         // update the window
  191.         
  192.     SWUpdateSpriteWorld(cur->spriteWorldP);
  193.     SWProcessSpriteWorld(cur->spriteWorldP);
  194.     SWAnimateSpriteWorld(cur->spriteWorldP);
  195.     
  196.     }
  197.     
  198.     SetPort(savePort);
  199.  
  200.  
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. ///--------------------------------------------------------------------------------------
  209. // EndSimpleAnimation
  210. ///--------------------------------------------------------------------------------------
  211.  
  212. void EndSimpleAnimation (void)
  213. {
  214.  
  215.     
  216.     //
  217.     // STEP #7: unlock the sprite world
  218.     //
  219.  
  220.     SWUnlockSpriteWorld(cur->spriteWorldP);
  221.  
  222.     //
  223.     // STEP #8: dispose of the pieces we created
  224.     //
  225.  
  226.     for (cur->spriteNum = 0; cur->spriteNum < kNumberOfSprites; (cur->spriteNum)++)
  227.     {
  228.         SWDisposeSprite(cur->simpleSpriteArray[cur->spriteNum], cur->spriteNum == 0);
  229.     }
  230.  
  231.     SWDisposeSpriteLayer(cur->spriteLayerP);
  232.     SWDisposeSpriteWorld(cur->spriteWorldP);
  233.  
  234.  
  235.     //
  236.     // STEP #9: shut down the sprite world package
  237.     //
  238.  
  239.     SWExitSpriteWorld();
  240.  
  241.  
  242. }
  243.  
  244.  
  245. ///--------------------------------------------------------------------------------------
  246. // ChangeMoveSpeed
  247. ///--------------------------------------------------------------------------------------
  248.  
  249. void ChangeMoveSpeed (void)
  250. {    
  251.     SWUnlockSpriteWorld(cur->spriteWorldP);
  252.     for (cur->spriteNum = 0; cur->spriteNum < kNumberOfSprites; cur->spriteNum++)
  253.         {
  254.         cur->simpleSpriteP = cur->simpleSpriteArray[cur->spriteNum];
  255.         cur->moveSpeed     = cur->moveSpeedArray[cur->spriteNum];
  256.         
  257.         SWSetSpriteMoveTime(cur->simpleSpriteP, (long)cur->moveSpeed);
  258.         SWSetSpriteFrameTime(cur->simpleSpriteP, (long)cur->moveSpeed);
  259.         }
  260.  
  261.     SWLockSpriteWorld(cur->spriteWorldP);
  262. }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.